package com.basic.library.utils;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.basic.library.Interface.MCallback;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import com.basic.library.base.BaseConstants;
import com.basic.library.bean.UserInfo;
import com.basic.library.utils.toast.ToastCompat;


/**
 * 本地缓存数据
 */
public class LocalData {

    private static LocalData localData = null;
    UserInfo userInfo = null;
    public ObservableField<UserInfo> user = new ObservableField<>();

    static {
        getInstance().user.set(getInstance().getUserInfo());
    }

    public static synchronized LocalData getInstance() {
        if (localData == null) {
            localData = new LocalData();
        }
        return localData;
    }

    public boolean isLogin() {
        userInfo = localData.getUserInfo();
        if (userInfo != null && userInfo.getToken() != null && userInfo.getToken().length() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查登录状态，并进入activity
     *
     * @param c
     */
    public static void gotoActivityAndCheckLogin(Context context, Class c, boolean needToast) {
        if (LocalData.getInstance().getUserInfo().getToken() != null) {
            Intent intent = new Intent();
            intent.setClass(context, c);
            context.startActivity(intent);
        } else {
            if (needToast) {
                ToastCompat.show("请先登录");
            }
            ARouter.getInstance().build("/app/LoginActivity").navigation();
        }
    }

    public UserInfo getUserInfo() {
        if (userInfo == null) {
            //读取本地
            String jsonString_lastLogin = PreferencesUtils.getStringPreferences(BaseConstants.AccountManager_NAME, BaseConstants.SharedPreferences_LoginInfo, null);
            if (jsonString_lastLogin != null && !jsonString_lastLogin.equals("")) {
                try {
                    userInfo = new Gson().fromJson(jsonString_lastLogin, new TypeToken<UserInfo>() {
                    }.getType());
                } catch (JsonParseException e) {
                    e.printStackTrace();
                    userInfo = new UserInfo();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    userInfo = new UserInfo();
                }
            } else {
                userInfo = new UserInfo();
            }
        }
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        if (callback != null)
            callback.onResponse(getUserInfo());
        if (userInfo == null) {
            PreferencesUtils.setStringPreferences(BaseConstants.AccountManager_NAME, BaseConstants.SharedPreferences_LoginInfo, "");
        } else {
            PreferencesUtils.setStringPreferences(BaseConstants.AccountManager_NAME, BaseConstants.SharedPreferences_LoginInfo, new Gson().toJson(userInfo));
        }
        this.userInfo = userInfo;
        user.set(userInfo);
        user.notifyChange();
    }

    private MCallback<UserInfo> callback;

    public void setCallback(MCallback<UserInfo> callback) {
        this.callback = callback;
    }
}
